package com.onion.core.context;

import com.onion.utils.file.FileUtils;
import com.onion.utils.file.PropertiesUtils;
import com.onion.utils.utils.StringHelper;
import com.onion.utils.utils.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class OnionConfig implements InitBean{

    private static Logger logger = LoggerFactory.getLogger(OnionConfig.class);
    private Map<String,String> configItemMap = new HashMap<>();

    private OnionConfig(){}

    public static OnionConfig instance(){
        return Inner.instance;
    }

    @Override
    public void init() {
        String classPath = SystemUtils.getClassesRootPath();
        this.loadProperties(classPath, true);
    }

    /**
     * 加载指定目录properites配置文件
     * @param path
     * @param isSubDir
     */
    public void loadProperties(String path, boolean isSubDir){
        List<String> properties = FileUtils.seekFiles(path, isSubDir);
        for(String current : properties){
            if(current.endsWith(".properties")){
                Properties props = PropertiesUtils.loadProperties(current);
                registerConfigItems(props);
                logger.info("已加载配置文件：{}", current);
            }
        }
    }

    public synchronized void registerConfigItems(Properties props){
        Set<Object> keyset = props.keySet();
        keyset.forEach(key->{
            String curKey = (String)key;
            String value = props.getProperty((String)key);
            if(!configItemMap.containsKey(curKey)){
                configItemMap.put(curKey, value);
            }else{
                configItemMap.put(curKey, value);
                logger.warn("配置项被覆盖：key={}, value={}", curKey, value);
            }
        });
    }

    public static String get(String key){
        String value = Inner.instance.configItemMap.get(key);
        if(StringHelper.checkBlank(value)){
            throw new RuntimeException("未找到配置项："+key);
        }else{
            return value.trim();
        }
    }

    public static String getString(String key){
        return Inner.instance.configItemMap.get(key);
    }

    public static String getString(String key, String defaultValue){
        return Inner.instance.configItemMap.getOrDefault(key, defaultValue);
    }

    public static Integer getInteger(String key){
        String value = Inner.instance.configItemMap.get(key);
        if(StringHelper.checkBlank(value)){
            return null;
        }
        return Integer.parseInt(value.trim());
    }

    public static Integer getInteger(String key, Integer defaultValue){
        String value = Inner.instance.configItemMap.get(key);
        if(StringHelper.checkBlank(value)){
            return defaultValue;
        }
        return Integer.parseInt(value.trim());
    }

    public static Long getLong(String key){
        String value = Inner.instance.configItemMap.get(key);
        if(StringHelper.checkBlank(value)){
            return null;
        }
        return Long.parseLong(value.trim());
    }

    public static Long getLong(String key, Long defaultValue){
        String value = Inner.instance.configItemMap.get(key);
        if(StringHelper.checkBlank(value)){
            return defaultValue;
        }
        return Long.parseLong(value.trim());
    }

    private static class Inner{
        private static OnionConfig instance = new OnionConfig();
    }

}